package com.gepengjun.lims.util;

import com.gepengjun.lims.entity.Orgnazation;
import com.gepengjun.lims.util.xssf.XSSFUtil;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

public class FileUtil {

    public static <T> XSSFWorkbook downloadUserAddTemplate(HttpServletResponse response, List<Orgnazation> companyList) throws IOException{
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        XSSFUtil util = new XSSFUtil(xssfWorkbook);
        //用户数据模板
        Sheet userSheet = util.createSheet("用户表",7);
        CellStyle DatecellStyle = xssfWorkbook.createCellStyle();
        DatecellStyle.setDataFormat(xssfWorkbook.createDataFormat().getFormat("yyyy-MM-dd"));
        userSheet.setDefaultColumnStyle(3,DatecellStyle);
        CellStyle intCellStyle = xssfWorkbook.createCellStyle();

        intCellStyle.setDataFormat(xssfWorkbook.createDataFormat().getFormat("0"));
        userSheet.setDefaultColumnStyle(2,intCellStyle);
        userSheet.setDefaultColumnStyle(5,intCellStyle);
        userSheet.setDefaultColumnStyle(6,intCellStyle);
        Row userTitleRow = util.createRow(userSheet,0,(short)500);
        util.createTitleCell(userTitleRow,0).setCellValue("用户名");
        util.createTitleCell(userTitleRow,1).setCellValue("姓名");
        util.createTitleCell(userTitleRow,2).setCellValue("手机号码");
        util.createTitleCell(userTitleRow,3).setCellValue("出生日期/yyyy-MM-dd");
        util.createTitleCell(userTitleRow,4).setCellValue("性别");
        util.createTitleCell(userTitleRow,5).setCellValue("部门编码");
        util.createTitleCell(userTitleRow,6).setCellValue("是否启用/0:不启用,1:启用");

        //组织编码表
        Sheet orgSheet = util.createSheet("组织信息表",3);
        Row orgTitleRow = util.createRow(orgSheet,0,(short)500);
        util.createTitleCell(orgTitleRow,0).setCellValue("公司名称");
        util.createTitleCell(orgTitleRow,1).setCellValue("部门名称");
        util.createTitleCell(orgTitleRow,2).setCellValue("部门编码");

        if (companyList != null && companyList.size() > 0){
            int start = 1;
            for (Orgnazation company:companyList){
                List<Orgnazation> deptList = company.getChildren();

                if (deptList != null && deptList.size() > 0){
                    orgSheet.addMergedRegion(new CellRangeAddress(start,start+company.getChildren().size()-1,0,0));

                    for (int i=0; i < deptList.size(); i++){
                        Orgnazation dept = deptList.get(i);
                        Row row = util.createRow(orgSheet,i+start);
                        Cell companyNameCell = util.createCell(row,0,CellType.STRING);
                        companyNameCell.setCellValue(company.getOrgName());
                        Cell deptNameCell = util.createCell(row,1,CellType.STRING);
                        deptNameCell.setCellValue(dept.getOrgName());
                        Cell deptCode = util.createCell(row,2,CellType.NUMERIC);
                        deptCode.setCellValue(dept.getOrgId());
                    }
                    start += deptList.size();
                }
            }
        }
        response.setContentType("application/application/vnd.ms-excel");
        response.setHeader("Content-disposition",
                "attachment;filename=" + URLEncoder.encode("模板.xlsx", "UTF-8"));
        OutputStream outputStream = response.getOutputStream();
        xssfWorkbook.write(outputStream);
        xssfWorkbook.close();
        outputStream.flush();
        outputStream.close();
        return  xssfWorkbook;
    }

    public static Boolean saveFile(String filePaht,String fileName,byte[] fileBytes) throws IOException{
        File fileDir = new File(filePaht);
        if (!fileDir.exists()){
            fileDir.mkdir();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(filePaht+fileName);
        fileOutputStream.write(fileBytes);
        fileOutputStream.flush();
        fileOutputStream.close();
        return true;
    }

}
